package com.amado.easyui2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amado.easyui2.constant.GlobalConstants;
import com.amado.easyui2.domain.Department;
import com.amado.easyui2.service.DepartmentService;
import com.amado.easyui2.service.impl.DepartmentServiceImpl;
import com.amado.easyui2.util.JsonUtil;
import com.amado.easyui2.vo.Page;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DepartmentServlet extends HttpServlet {

	private DepartmentService departmentService = new DepartmentServiceImpl();
	//
	/**
	 * 
	 */
	private static ObjectMapper mapper = new ObjectMapper();
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		arg1.setHeader("Content-type", "text/html;charset=UTF-8");
		String method = arg0.getParameter("method");
		if ("add".equals(method)) {
			String strJson = arg0.getParameter("data");
			String deleteFlag = "N";
			String createName = (String) arg0.getSession().getAttribute(GlobalConstants.SESSION_USER);
			Department userInfo = JsonUtil.jsonToBean(strJson, Department.class);
			userInfo.setDeleteFlag(deleteFlag);
			userInfo.setCreateName(createName);
			departmentService.add(userInfo);
		} else if ("delete".equals(method)) {
			delete(arg0, arg1);
		} else if ("query".equals(method)) {
			query(arg0, arg1);
		} else if ("queryAll".equals(method)) {
			queryAll(arg0, arg1);
		} else if ("edit".equals(method)) {
			edit(arg0, arg1);
		} else if ("update".equals(method)) {
			update(arg0, arg1);
		}
	}

	private void delete(ServletRequest arg0, HttpServletResponse arg1) throws IOException {
		String ids = arg0.getParameter("ids");
		if (ids != null && ids.trim() != "") {
			departmentService.delete(Long.valueOf(ids));
			arg1.getWriter().print(JsonUtil.beanToJson(new Result()));
		} else {
			arg1.getWriter().print(JsonUtil.beanToJson(new Result("0", "未找到数据")));
		}
	}

	class Result {
		// 0失败，1成功
		private String resultCode = "1";
		private String resultMessage = "成功";

		public Result() {
		}

		public Result(String resultCode, String resultMessage) {
			super();
			this.resultCode = resultCode;
			this.resultMessage = resultMessage;
		}

		public String getResultCode() {
			return resultCode;
		}

		public void setResultCode(String resultCode) {
			this.resultCode = resultCode;
		}

		public String getResultMessage() {
			return resultMessage;
		}

		public void setResultMessage(String resultMessage) {
			this.resultMessage = resultMessage;
		}
	}

	protected void edit(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Long id = Long.valueOf(arg0.getParameter("id"));
		List<Department> list = departmentService.list();
		for (Department department : list) {
			if (department.getId().equals(id)) {
				Department departmentId = department;
				PrintWriter out = arg1.getWriter();
				out.print(mapper.writeValueAsString(departmentId));
				break;
			}
		}
	}

	protected void query(HttpServletRequest arg0, HttpServletResponse resp) throws IOException {
		Page page = new Page();
		String strPage = arg0.getParameter("page");
		String strRows = arg0.getParameter("rows");
		int page1= strPage==null?1:Integer.parseInt(strPage);//
		int rows = strRows==null?5:Integer.parseInt(strRows);
		page.setCurrentPage(page1);
		page.setRowSize(rows);
		Page<Department> pageList = departmentService.listAll(page);
		PrintWriter out = resp.getWriter();
		out.print(mapper.writeValueAsString(pageList));
	}
	protected void queryAll(HttpServletRequest arg0, HttpServletResponse resp) throws IOException {
		List<Department> pageList = departmentService.listAll();
		PrintWriter out = resp.getWriter();
		out.print(mapper.writeValueAsString(pageList));
	}
	protected void update(HttpServletRequest arg0, HttpServletResponse resp) {
		String strJson = arg0.getParameter("data");// 解析
		Department userInfo = JsonUtil.jsonToBean(strJson, Department.class);
		Long id = userInfo.getId();
		String departmentNum = userInfo.getDepartmentNum();
		String departmentName = userInfo.getDepartmentName();
		String createName = userInfo.getCreateName();
		String updateName = userInfo.getUpdateName();
		Department department = new Department(id, departmentNum, departmentName, createName, updateName);
		departmentService.update(department);
	}
}
